package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.prefs.Preferences;
import java.util.prefs.PreferencesFactory;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Club;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Train;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.User;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.LoginModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.TrainModel;


/**
 * Created by Lix on 2018/9/27.
 */

public class LoginActivity  extends AppCompatActivity implements View.OnClickListener {
    private EditText userphone;
    private EditText password;
    private Button btn_login;
    private TextView btn_reg;
    private CheckBox checkBox;
    private ImageView mBgView1,mBgView2,mBgView3,mBgView4;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        remPassword();
        initData();
    }
    private void remPassword(){
        boolean isCheck=sp.getBoolean("remember_password",false);
        if(isCheck){
            userphone.setText(sp.getString("username","lx"));
            password.setText(sp.getString("password","11"));
            checkBox.setChecked(true);

        }
    }

    public void initViews(){
        userphone = (EditText)findViewById(R.id.ed_name);
        password = (EditText)findViewById(R.id.ed_pwd);
        btn_login = (Button)findViewById(R.id.login_btn);
        btn_reg = (Button)findViewById(R.id.reg_btn);
        mBgView1 = (ImageView)findViewById(R.id.login_bg_image1);
        mBgView2 = (ImageView)findViewById(R.id.login_bg_image2);
        mBgView3 = (ImageView)findViewById(R.id.login_bg_image3);
        mBgView4 = (ImageView)findViewById(R.id.login_bg_image4);
        checkBox = (CheckBox)findViewById(R.id.check_login);
        sp = PreferenceManager.getDefaultSharedPreferences(this);

    }
    public void initEvents(){
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.login_btn:
                final String name = userphone.getText().toString();
                final String pwd = password.getText().toString();
                if (name!=null&&"".equals(name)){
                    Toast.makeText(LoginActivity.this,"请输入帐号！", Toast.LENGTH_SHORT).show();
                }else if(pwd!=null&&"".equals(pwd)){
                    Toast.makeText(LoginActivity.this,"请输入密码！", Toast.LENGTH_SHORT).show();
                }else {
                    editor = sp.edit();
                  LoginModel loginModel = new LoginModel();
                  loginModel.login(name, pwd, new TListener<Success>() {
                      @Override
                      public void onResponse(Success success) {
                          if(success.getError().equals("0")){
                              if(checkBox.isChecked()){
                                  editor.putString("username",name);
                                  editor.putString("password",pwd);
                                  editor.putBoolean("remember_password",true);
                                  saveID(success.getUid());
                                  Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                  startActivity(intent);
                                  finish();

                              }else{
                                  editor.clear();
                                  saveID(success.getUid());
                                  Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                  startActivity(intent);
                                  finish();
                              }

                          }else{
                              Toast.makeText(LoginActivity.this,"用户名或密码错误", Toast.LENGTH_SHORT).show();
                          }
                          editor.apply();
                  }

                      @Override
                      public void onFail(String msg) {
                          Toast.makeText(LoginActivity.this,"网络请求失败", Toast.LENGTH_SHORT).show();
                      }
                  });


                }


                break;
            case R.id.reg_btn:
                Intent intent1 = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent1);
                break;
        }

    }

    public  void saveID(int user_id){
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("uid",user_id);
        editor.commit();
    }

    private void initData(){
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mBgView1, "alpha", 1.0f, 0f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mBgView2, "alpha", 0f, 1.0f);
        ObjectAnimator animatorScale1 = ObjectAnimator.ofFloat(mBgView1, "scaleX", 1.0f, 1.3f);
        ObjectAnimator animatorScale2 = ObjectAnimator.ofFloat(mBgView1, "scaleY", 1.0f, 1.3f);
        AnimatorSet animatorSet1 = new AnimatorSet();
        animatorSet1.setDuration(5000);
        animatorSet1.play(animator1).with(animator2).with(animatorScale1).with(animatorScale2);


        ObjectAnimator animator3 = ObjectAnimator.ofFloat(mBgView2, "alpha", 1.0f, 0f);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(mBgView3, "alpha", 0f, 1.0f);
        ObjectAnimator animatorScale3 = ObjectAnimator.ofFloat(mBgView2, "scaleX", 1.0f, 1.3f);
        ObjectAnimator animatorScale4 = ObjectAnimator.ofFloat(mBgView2, "scaleY", 1.0f, 1.3f);
        AnimatorSet animatorSet2 = new AnimatorSet();
        animatorSet2.setDuration(5000);
        animatorSet2.play(animator3).with(animator4).with(animatorScale3).with(animatorScale4);


        ObjectAnimator animator5 = ObjectAnimator.ofFloat(mBgView3, "alpha", 1.0f, 0f);
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(mBgView4, "alpha", 0f, 1.0f);
        ObjectAnimator animatorScale5 = ObjectAnimator.ofFloat(mBgView3, "scaleX", 1.0f, 1.3f);
        ObjectAnimator animatorScale6 = ObjectAnimator.ofFloat(mBgView3, "scaleY", 1.0f, 1.3f);
        AnimatorSet animatorSet3 = new AnimatorSet();
        animatorSet3.setDuration(5000);
        animatorSet3.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // 放大的View复位
                mBgView1.setScaleX(1.0f);
                mBgView1.setScaleY(1.0f);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet3.play(animator5).with(animator6).with(animatorScale5).with(animatorScale6);


        ObjectAnimator animator7 = ObjectAnimator.ofFloat(mBgView4, "alpha", 1.0f, 0f);
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(mBgView1, "alpha", 0f, 1.0f);
        ObjectAnimator animatorScale7 = ObjectAnimator.ofFloat(mBgView4, "scaleX", 1.0f, 1.3f);
        ObjectAnimator animatorScale8 = ObjectAnimator.ofFloat(mBgView4, "scaleY", 1.0f, 1.3f);
        AnimatorSet animatorSet4 = new AnimatorSet();
        animatorSet4.setDuration(5000);
        animatorSet4.play(animator7).with(animator8).with(animatorScale7).with(animatorScale8);


        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(animatorSet1, animatorSet2, animatorSet3, animatorSet4);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // 将放大的View 复位
                mBgView2.setScaleX(1.0f);
                mBgView2.setScaleY(1.0f);
                mBgView3.setScaleX(1.0f);
                mBgView3.setScaleY(1.0f);
                mBgView4.setScaleX(1.0f);
                mBgView4.setScaleY(1.0f);
                // 循环播放
                animation.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }

}
